import HTMLParser
import urllib
import re


urlString = "http://tix.estpak.ee/?members"
urlText = []
pathOut = "ixp_members.txt"


class Parse136 (HTMLParser.HTMLParser):
    
    check1 = False
    check2 = False
    
    def handle_starttag (self, tag, attrs):
        if tag == "li":
            self.check1 = True
            
    def handle_endtag (self, tag):
        # l'ASN si trova all'interno di un tag <li> dopo </a>
        if tag == "a" and self.check1 == True:
            self.check1 = False
            self.check2 = True
    
    def handle_data (self, data):
        if self.check2 == True:
            m = re.match("^ \(AS (\d+)\)$",data)
            if m:
                data = m.group(1)
                if data not in urlText:
                    urlText.append(data)
            self.check2 = False


lparser = Parse136()
lparser.feed(urllib.urlopen(urlString).read())
fileOut = open(pathOut, "a")
for item in urlText:
    #print item # debug
    print >> fileOut, "136 %s" % item
fileOut.close()
